package fr.afcepf.al22.implementations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.sql.DataSource;








import com.mysql.jdbc.PreparedStatement;

import fr.afepf.al22.qualite.entity.Personne;
import fr.afepf.al22.qualite.interfaces.IDaoPersonne;
/**
 * Implementation de DAOPeronne
 */
public class DaoPersonneImpl implements IDaoPersonne {
    /**
     * DataSource
     */
    DataSource ds=new Al22DataSource();
    /**
     * query ajout personne
     */
    private String reqAjout="INSERT INTO personne (nom,mail,mdp,naissance) VALUES(?,?,?,?)";
    /**
     * Indice du paramatre de la requete
     */
    private final int indicenomajout=1;
    private final int indiceMailajout=2;
    private final int indiceMdpajout=3;
    private final int indiceNaissanceajout=4;
    @Override
    public Personne ajouter(Personne paramPers) {
        Connection cnx=null;
        try {
            Integer constant=1;
            cnx=ds.getConnection();
            PreparedStatement pstmt=(PreparedStatement) cnx.prepareStatement(reqAjout, Statement.RETURN_GENERATED_KEYS);
            pstmt.setString(indicenomajout, paramPers.getNom());
            pstmt.setString(indiceMdpajout, paramPers.getMdp());
            pstmt.setString(indiceMailajout, paramPers.getMail());
            pstmt.setDate(indiceNaissanceajout, new java.sql.Date(paramPers.getDate().getTime()));

            int nbLigne=pstmt.executeUpdate();
            ResultSet rsKey=pstmt.getGeneratedKeys();
            if (nbLigne==1 && rsKey.next())
            {
                paramPers.setId(rsKey.getInt(constant));
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                cnx.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
       return paramPers;
    }

    @Override
    public List<Personne> rechercher(String paramNom) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Personne verifMail(String paramMail) {
      
        return null;
    }

}
